#pragma once
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <vector>
#include "svm.h"
#include <SPI.h>
#include <SD.h>
#include "tools.h"

//extern vector<double> rollBackProb;
//extern vector<double> commonCharacterProb;
//extern bool isRollBack;
//extern bool isCommonCharacter;

int print_null(const char *s, ...) { return 0; }

static int(*info)(const char *fmt, ...) = &printf;

struct svm_node *x;
int max_nr_attr = 64;
double predict_label;
double tmpMaxPredict = 0.0;

struct svm_model* model;
int predict_probability = 1; // default:0

static char *line = NULL;
static int max_line_len;

static char* readline(File input)
{
	int len;

	// if (fgets(line, max_line_len, input) == NULL)
  if (!readLineFromSD(input, max_line_len, line))
		return NULL;

	while (strrchr(line, '\n') == NULL)
	{
		max_line_len *= 2;
		line = (char *)realloc(line, max_line_len);
		len = (int)strlen(line);
    if (!readLineFromSD(input, max_line_len - len, line + len));
		// if (fgets(line + len, max_line_len - len, input) == NULL)
			break;
	}
	return line;
}

void exit_input_error(int line_num)
{
	// fprintf(stderr, "Wrong input format at line %d\n", line_num);
  Serial.println("Wrong input format at line " + String(line_num));
	// exit(1);
}

// void predict(FILE* input, FILE* output)
void predict(SDLib::File input, SDLib::File output)
{
  Serial.println("begin to predict");
	int correct = 0;
	int total = 0;
	double error = 0;
	double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;

	int svm_type = svm_get_svm_type(model);
  Serial.println("svm_type: "+String(svm_type));
	int nr_class = svm_get_nr_class(model);
  Serial.println("nr_class: "+String(nr_class));
	double *prob_estimates = NULL;
	int j;
	tmpMaxPredict = 0;

	if (predict_probability)
	{
		if (svm_type == NU_SVR || svm_type == EPSILON_SVR)
		{

		}
			//info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n", svm_get_svr_probability(model));
		else
		{
			int *labels = (int *)malloc(nr_class * sizeof(int));
			svm_get_labels(model, labels);
			prob_estimates = (double *)malloc(nr_class * sizeof(double));
			// fprintf(output, "labels");
      Serial.print("labels ");
			// cout << "labels "; // cout
			for (j = 0; j < nr_class; j++) {
				// fprintf(output, " %d", labels[j]);
        Serial.print(labels[j]);
        Serial.print(" ");
				// cout << gesture[labels[j]] << "(" << char('a' + labels[j]) << ")" << " "; // cout
			}
			// cout << endl; // cout
			// fprintf(output, "\n");
      Serial.print("\n");
			free(labels);
		}
	}

	max_line_len = 1024;
  // max_line_len = 6144;
	line = (char *)malloc(max_line_len * sizeof(char));
	while (readline(input) != NULL)
	{
		int i = 0;
		double target_label;
		char *idx, *val, *label, *endptr;
		int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0

		label = strtok(line, " \t\n");
		if (label == NULL) {// empty line
      Serial.println("error1");
			exit_input_error(total + 1);
    }
		target_label = strtod(label, &endptr);
		if (endptr == label || *endptr != '\0'){
      Serial.println("error2");
			exit_input_error(total + 1);
    }
		while (1)
		{
			if (i >= max_nr_attr - 1)	// need one more for index = -1
			{
				max_nr_attr *= 2;
				x = (struct svm_node *) realloc(x, max_nr_attr * sizeof(struct svm_node));
			}

			idx = strtok(NULL, ":");
			val = strtok(NULL, " \t");

			if (val == NULL)
				break;
			errno = 0;
			x[i].index = (int)strtol(idx, &endptr, 10);
			if (endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) {
        Serial.println("error3");
				exit_input_error(total + 1);
      }
			else
				inst_max_index = x[i].index;

			errno = 0;
			x[i].value = strtod(val, &endptr);
			if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) {
        Serial.println("error4");
				exit_input_error(total + 1);
      }

			++i;
		}
		x[i].index = -1;
		if (predict_probability && (svm_type == C_SVC || svm_type == NU_SVC))
		{
      // Serial.println("print result");
			predict_label = svm_predict_probability(model, x, prob_estimates);

			// fprintf(output, "%g", predict_label);
      Serial.print(predict_label);
      Serial.print(" ");
			// cout << "???label???????"; // cout
			for (j = 0; j < nr_class; j++) {
				// fprintf(output, " %g", prob_estimates[j]);
        Serial.print(prob_estimates[j]);
        Serial.print(" ");
				tmpMaxPredict = tmpMaxPredict > prob_estimates[j] ? tmpMaxPredict : prob_estimates[j];
				// cout << prob_estimates[j] * 100 << "%"; // cout prob_estimates
				if (j != nr_class - 1) {
					// cout << " ";
				}
			}
			// cout << endl << "???????" << gesture[int(predict_label)] << "(" << char('a' + int(predict_label)) << "):" << tmpMaxPredict * 100 << "%" << endl;

			// fprintf(output, "\n");
      Serial.print("\n");
		}
		else
		{
			predict_label = svm_predict(model, x);
			// fprintf(output, "%.17g\n", predict_label);
      Serial.println(predict_label);
		}

	if (predict_label == target_label)
		++correct;
		error += (predict_label - target_label)*(predict_label - target_label);
		sump += predict_label;
		sumt += target_label;
		sumpp += predict_label*predict_label;
		sumtt += target_label*target_label;
		sumpt += predict_label*target_label;
		++total;
	}
	if (svm_type == NU_SVR || svm_type == EPSILON_SVR)
	{
		/*info("Mean squared error = %g (regression)\n", error / total);
		info("Squared correlation coefficient = %g (regression)\n",
			((total*sumpt - sump*sumt)*(total*sumpt - sump*sumt)) /
			((total*sumpp - sump*sump)*(total*sumtt - sumt*sumt))
		);*/
	}
	/*else
		info("Accuracy = %g%% (%d/%d) (classification)\n",
		(double)correct / total * 100, correct, total);*/
	if (predict_probability)
		free(prob_estimates);
}

void exit_with_help()
{
	printf(
			"Usage: svm-predict [options] test_file model_file output_file\n"
			"options:\n"
			"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported\n"
			"-q : quiet mode (no outputs)\n"
	);
	exit(1);
}

int svmPredict(int argc, char** argv)
{
  Serial.println("begin to predict");
	argc = 4;

	// FILE *input, *output;
  // SDLib::File input, output;
	int i;
	// parse options
	for (i = 1;i<argc;i++)
	{
		if (argv[i][0] != '-') break;
		++i;
		switch (argv[i - 1][1])
		{
			case 'b':
				predict_probability = atoi(argv[i]);
				break;
			case 'q':
				//info = &print_null;
				i--;
				break;
			default:
				// fprintf(stderr, "Unknown option: -%c\n", argv[i - 1][1]);
        Serial.println("Unknown option: -" + String(argv[i - 1][1]));
				exit_with_help();
		}
	}

	if (i >= argc - 2)
		exit_with_help();

	// input = fopen(argv[i], "r");
  // Serial.println("1");
  SDLib::File input = SD.open(argv[i], FILE_READ);
  // Serial.println(SD.exists(argv[i]));
  // Serial.println("2");
	// if (input == NULL)
  if (!input)
	{
    Serial.print("Error opening ");
    Serial.println(argv[i]);
		// fprintf(stderr, "can't open input file %s\n", argv[i]);
		// exit(1);
	}
  Serial.println("input file init success");

	// output = fopen(argv[i + 2], "w");
  SDLib::File output = SD.open(argv[i + 2], FILE_WRITE);
  // Serial.println(SD.exists(argv[i + 2]));
	// if (output == NULL)
  if (!output)
	{
    Serial.print("Error opening ");
    Serial.println(argv[i + 2]);
		// fprintf(stderr, "can't open output file %s\n", argv[i + 2]);
		// exit(1);
	}
  Serial.println("result file init success");

  // TODO 加载模型
  model = svm_load_model(argv[i + 1]);
  Serial.println("7");
	if (model == 0)
	{
    Serial.print("can't open model file:");
    Serial.println(argv[i + 1]);
		// fprintf(stderr, "can't open model file %s\n", argv[i + 1]);
		// exit(1);
	}
  Serial.println("0");

	x = (struct svm_node *) malloc(max_nr_attr * sizeof(struct svm_node));
  Serial.println("1");
	if (predict_probability)
	{
    Serial.println("2");
		if (svm_check_probability_model(model) == 0)
		{
      Serial.println("3");
			// fprintf(stderr, "Model does not support probabiliy estimates\n");
			// exit(1);
		}
	}
	else
	{
    Serial.println("4");
		if (svm_check_probability_model(model) != 0)
		{
      Serial.println("5");

		}
		//info("Model supports probability estimates, but disabled in prediction.\n");
	}
  Serial.println("6");
  
  Serial.println("predict(input, output) begin");
  // TODO 进行推理
	predict(input, output);
  Serial.println("predict(input, output) end");
	svm_free_and_destroy_model(&model);
	free(x);
	free(line);
	// fclose(input);
	// fclose(output);
  input.close();
  output.close();
	return 0;
}

